package com.brspace.qualitycontrol.controller.wps;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.brspace.qualitycontrol.base.Response;
import com.brspace.qualitycontrol.entity.dto.FileReqDTO;
import com.brspace.qualitycontrol.service.FileService;
import com.brspace.qualitycontrol.service.impl.FileServiceImpl;
import com.brspace.qualitycontrol.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author zm
 * 文件相关回调接口
 */
@RestController
@CrossOrigin
@RequestMapping("v1/3rd/file")
@Api(description = "wps-文件管理接口")
public class FileCallBackController {

    @Autowired
    public FileCallBackController(FileServiceImpl fileServiceImpl) {
        this.fileServiceImpl = fileServiceImpl;
    }

    private final FileServiceImpl fileServiceImpl;

    /**
     * 获取文件元数据
     */
    @ApiOperation(value = "获取文件元数据", notes = "获取文件元数据(_w_filetype:web)")
    @GetMapping("info")
    public ResponseEntity<Object> getFileInfo(
            String w_fileId, HttpServletRequest request
    ) {
        String fileId = request.getHeader("x-weboffice-file-id");
        String token = request.getHeader("x-wps-weboffice-token");
        System.out.println("fileId=====================>" + fileId);
        System.out.println("token=====================>" + token);
        try {
            Map<String, Object> map = null;

            map = fileServiceImpl.getFileInfo(fileId);

            return Response.success(map);
        } catch (Exception e) {
            return Response.bad("获取文件元数据异常");
        }
    }

    /**
     * 通知此文件目前有哪些人正在协作
     */
    @ApiOperation(value = "通知此文件目前有哪些人正在协作", notes = "通知此文件目前有哪些人正在协作")
    @PostMapping("online")
    public ResponseEntity<Object> fileOnline(
            @RequestBody JSONObject obj
    ) {
//        logger.info("通知此文件目前有哪些人正在协作param:{}", JSON.toJSON(obj));
        return Response.success();
    }

    /**
     * 上传文件新版本
     */
    @ApiOperation(value = "上传文件新版本", notes = "上传文件新版本")
    @PostMapping("save")
    public ResponseEntity<Object> fileSave(
            @RequestBody MultipartFile file,
            String _w_userid
    ) {
//        logger.info("上传文件新版本");
        Map<String, Object> map = fileServiceImpl.fileSave(file, _w_userid);
        return Response.success(map);
    }

    /**
     * 获取特定版本的文件信息
     */
    @ApiOperation(value = "获取特定版本的文件信息", notes = "获取特定版本的文件信息")
    @GetMapping("version/{version}")
    public ResponseEntity<Object> fileVersion(
            @PathVariable Integer version
    ) {
//        logger.info("获取特定版本的文件信息version:{}", version);
        Map<String, Object> res = fileServiceImpl.fileVersion(version);
        return Response.success(res);
    }

    /**
     * 文件重命名
     */
    @ApiOperation(value = "文件重命名", notes = "文件重命名")
    @PutMapping("rename")
    public ResponseEntity<Object> fileRename(
            @RequestBody FileReqDTO req,
            String _w_userid
    ) {
//        logger.info("文件重命名param:{},userId:{}", JSON.toJSON(req), _w_userid);
        fileServiceImpl.fileRename(req.getName(), _w_userid);
        return Response.success();
    }

    /**
     * 获取所有历史版本文件信息
     */
    @ApiOperation(value = "获取所有历史版本文件信息", notes = "获取所有历史版本文件信息")
    @PostMapping("history")
    public ResponseEntity<Object> fileHistory(
            @RequestBody FileReqDTO req
    ) {
//        logger.info("获取所有历史版本文件信息param:{}", JSON.toJSON(req));
        Map<String, Object> result = fileServiceImpl.fileHistory(req);
        return Response.success(result);
    }

    /**
     * 新建文件
     */
    @ApiOperation(value = "新建文件", notes = "新建文件")
    @PostMapping("new")
    public ResponseEntity<Object> fileNew(
            @RequestBody MultipartFile file,
            String _w_userid
    ) {
//        logger.info("新建文件_w_userid:{}", _w_userid);
        Map<String, Object> res = fileServiceImpl.fileNew(file, _w_userid);
        return Response.success(res);
    }

    /**
     * 文件格式转换回调--wps官方回掉用
     */
    @PostMapping("convertCallback")
    @ApiOperation(value = "文件格式转换回调--wps官方回掉用", notes = "文件格式转换回调--wps官方回掉用")
    public ResponseEntity<Object> callback(
            HttpServletRequest request
    ) {
//        logger.info("文件转换回掉");
        fileServiceImpl.convertCallBack(request);
        return Response.success();
    }

}
